import BaseService from './base';
import Sequelize from 'sequelize';
import db from '../models';
const Op = Sequelize.Op;

export default class SumsService extends BaseService {
  constructor({ sumsDao }) {
    super();
    this.sumsDao = sumsDao
  }

  // 总求和（总访问量、售罄访问量、无效访问量、请求IP数量、有效IP请求、无效IP请求）
  async getAllSum () {
    try {
      const rows = await db.sequelize.query('CALL sumAll();', {
        type: db.sequelize.QueryTypes.SELECT
      })
      return this.success('获取成功', rows);
    } catch (err) {
      return this.fail('获取失败', null, err);
    }
  }

  // 今日总求和（总访问量、售罄访问量、无效访问量、请求IP数量、有效IP请求、无效IP请求）
  async getDaySum (sums) {
    try {
      const rows = await db.sequelize.query('set @days="'+ sums.days +'";CALL sumDay(@days);', {
        type: db.sequelize.QueryTypes.SELECT
      })
      return this.success('获取成功', rows);
    } catch (err) {
      return this.fail('获取失败', null, err);
    }
  }

  // 根据条件查询访问统计数据
  async getSearchSum (sums) {
    try {
      const res = await db.sequelize.query('set @tno="'+ sums.t_no +'";CALL sumSearch(@tno);', {
        type: db.sequelize.QueryTypes.SELECT
      })
      return this.success('获取成功', res);
    } catch (err) {
      return this.fail('获取失败', null, err);
    }
  }

  // 批量添加
  async bulkSums (sums) {
    const [err, list] = await this.sumsDao.bulkSums(sums);
    if (err) {
      return this.fail('批量添加失败', null, err);
    }
    return this.success('批量添加成功', list);
  }

  // 总请求次数
  async incSum (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_sum: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 代理IP消耗量
  async incSip (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_sip: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 无效IP
  async incWip (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_wip: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 虚拟页面
  async incXny (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_xny: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 链接关闭
  async incClose (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_close: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 查询超时
  async incCtime (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_ctime: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 无效查询
  async incWsearch (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_wsearch: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 已售罄
  async incOver (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_over: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 查询成功
  async incSearch (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_search: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 无效登录
  async incWlogo (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_wlogo: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 登录成功
  async incLogo (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_logo: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 登录失败
  async incFlogo (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_flogo: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 下单成功
  async incSuccess (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_success: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  // 下单失败
  async incLoser (sums) {
    const [err, updateSums] = await this.sumsDao.increment({
      t_loser: +1
    }, {
      where: {
        t_no: sums.t_no,
        t_create: sums.t_create,
        mark: { [Op.ne]: sums.mark }
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }

  async getSums (sums) {
    const [err, list] = await this.sumsDao.getSumsAll({
      where: {
        mark: {[Op.ne]: sums.mark}
      }
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', list);
  }

  async getSumsAll (sums) {
    const [err, list] = await this.sumsDao.getSumsAll({
      where: {
        mark: {[Op.ne]: sums.mark}
      }
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', list);
  }

  async getSumsList(sums) {
    const [err, count, rows] = await this.sumsDao.getSumsList({
      where: {
        t_name: sums.t_name === '' ? {
          [Op.ne]: null
        } : sums.t_name,
        mark: {[Op.ne]: sums.mark}
      },
      limit: sums.limit,
      offset: sums.limit * (sums.offset - 1),
      distinct: true
    });
    if (err) {
      return this.fail('获取列表失败', null, err);
    }
    return this.success('查询成功', count, rows);
  }

  async addSums(sums) {
    const [addErr] = await this.sumsDao.addSums(sums);
    if (addErr) {
      return this.fail('添加失败', null, err);
    }
    return this.success('添加成功')
  }

  async delSums(sums) {
    const [err, updateSums] = await this.sumsDao.update(sums, {
      where: {
        id: sums.id
      }
    });
    if (err) {
      return this.fail('删除失败');
    }
    return this.success('删除成功');
  }

  async updateSums(sums) {
    const [err, updateSums] = await this.sumsDao.update(sums, {
      where: {
        id: sums.id
      }
    });
    if (err) {
      return this.fail('更新失败');
    }
    return this.success('更新成功');
  }
}